using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HomePortal.Models;

namespace HomePortal.Controllers
{   
    public class PhotosController : Controller
    {
		private readonly IPhotoRepository photoRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public PhotosController() : this(new PhotoRepository())
        {
        }

        public PhotosController(IPhotoRepository photoRepository)
        {
			this.photoRepository = photoRepository;
        }


        [Authorize(Users = "admin")]
        public ViewResult Index()
        {
            return View(photoRepository.All);
        }

        public ViewResult Gallery()
        {
            return View(photoRepository.All);
        }

        public ViewResult Details(int id)
        {
            return View(photoRepository.Find(id));
        }


        [Authorize(Users = "admin")]
        public ActionResult Create()
        {
            return View();
        } 

        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Users = "admin")]
        public ActionResult Create(Photo photo, IEnumerable<HttpPostedFileBase> image)
        {
            if (ModelState.IsValid)
            {
                var images = image as HttpPostedFileBase[] ?? image.ToArray();
                if (images[0] != null)
                    {
                        photo.ImageMiniMimeType = images[0].ContentType;
                        photo.ImageMini = new byte[images[0].ContentLength];
                        images[0].InputStream.Read(photo.ImageMini, 0, images[0].ContentLength);
                    }
                if (images[1] != null)
                    {
                        photo.ImageMimeType = images[1].ContentType;
                        photo.ImageData = new byte[images[1].ContentLength];
                        images[1].InputStream.Read(photo.ImageData, 0, images[1].ContentLength);
                    }
                
                photoRepository.InsertOrUpdate(photo);
                photoRepository.Save();
                return RedirectToAction("Index");
            }
            return View();
        }


        [Authorize(Users = "admin")]
        public ActionResult Edit(int id)
        {
             return View(photoRepository.Find(id));
        }

        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Users = "admin")]
        public ActionResult Edit(Photo photo, HttpPostedFileBase image)
        {
            if (ModelState.IsValid) {
                if (image != null)
                {
                    photo.ImageMimeType = image.ContentType;
                    photo.ImageData = new byte[image.ContentLength];
                    image.InputStream.Read(photo.ImageData, 0, image.ContentLength);
                }
                photoRepository.InsertOrUpdate(photo);
                photoRepository.Save();
                return RedirectToAction("Index");
            }
            return View();
        }

        [Authorize(Users = "admin")]
        public ActionResult Delete(int id)
        {
            return View(photoRepository.Find(id));
        }

        [Authorize(Users = "admin")]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            photoRepository.Delete(id);
            photoRepository.Save();

            return RedirectToAction("Index");
        }
        
        [HttpGet]
        public FileContentResult GetImage(int photoId)
        {
            var photo = photoRepository.Find(photoId);
            if (photo != null)
            {
                return File(photo.ImageData, photo.ImageMimeType);
            }
            return null;
        }

        [HttpGet]
        public FileContentResult GetMiniImage(int photoId)
        {
            var photo = photoRepository.Find(photoId);
            if (photo != null)
            {
                return File(photo.ImageMini, photo.ImageMiniMimeType);
            }
            return null;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                photoRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

